package com.wisdytech.ccdserver.common.es;

import com.wisdytech.ccdserver.bucket.dto.BucketDto;
import com.wisdytech.ccdserver.common.contants.BucketContants;
import com.wisdytech.ccdserver.matchwarning.dto.WarningSandstoneImageDto;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


import java.io.IOException;

/**
 * @author sunjinxin
 * @date 2024/5/21
 */
@Component
public class EsOperationUtils {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * @Description: 根据目录，桶名称，时间查询
     * @Param: * @param bucketDto
     * @return: * @return org.elasticsearch.search.SearchHit[]
     * @Author: SV00216491
     * @Date: 2024/3/29  11:14
     */

    public SearchHit[] searchByFilter(BucketDto bucketDto) throws IOException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("origin_mtime");
//        rangeQueryBuilder.gte("2019-07-06T16:00:00.000Z");
////        rangeQueryBuilder.lte("2019-09-08T16:00:00.000Z");
        rangeQueryBuilder.gte("2023-10-20T00:00:00.000Z");
        rangeQueryBuilder.lte("2023-10-27T23:00:00.000Z");
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        boolBuilder.must(QueryBuilders.termQuery("bucket", "JT03-QD-L3-03"));
        boolBuilder.must(rangeQueryBuilder);
        sourceBuilder.query(boolBuilder);
        SearchRequest searchRequest = new SearchRequest(BucketContants.SEARCHINDEXNAME);
        searchRequest.types(BucketContants.INDEX_TPYE);
        searchRequest.source(sourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest);
        SearchHit[] searchHits = response.getHits().getHits();
        return searchHits;
    }

    public long getImageNumByDirectory(WarningSandstoneImageDto warningSandstoneImageDto) throws IOException {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("origin_mtime");
//        rangeQueryBuilder.gte("2019-07-06T16:00:00.000Z");
////        rangeQueryBuilder.lte("2019-09-08T16:00:00.000Z");
        rangeQueryBuilder.gte(warningSandstoneImageDto.getOriginMtimeBegin());
        rangeQueryBuilder.lte(warningSandstoneImageDto.getOriginMtimeEnd());
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        boolBuilder.filter(rangeQueryBuilder);
        //boolBuilder.must(QueryBuilders.termQuery("bucket", "JT03-QD-L3-03"));
        boolBuilder.filter(QueryBuilders.prefixQuery("name.keyword", warningSandstoneImageDto.getBucketDirectory()));
        sourceBuilder.query(boolBuilder);
        SearchRequest searchRequest = new SearchRequest(BucketContants.SEARCHINDEXNAME);
        searchRequest.types(BucketContants.INDEX_TPYE);
        sourceBuilder.size(0);
        searchRequest.source(sourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest);

        return response.getHits().totalHits;
    }
}
